﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmMovieAdmin : Form
    {
        public FrmMovieAdmin()
        {
            InitializeComponent();
        }

        //加载事件
        private void FrmMovieAdmin_Load(object sender, EventArgs e)
        {
            MovieManage.InitialMovie();
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = new BindingList<Movie>(MovieManage.dirMovie.Values.ToList());
        }

        #region 增删改查
        /// <summary>
        /// 临时集合 存储查找的电影
        /// </summary>
        Dictionary<string, Movie> dirmovie = new Dictionary<string, Movie>();
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            dirmovie.Clear();
            if (textBox1.Text.Trim().Length < 1)
            {
                BindData();
                return;
            }
            string name = textBox1.Text.Trim();
            foreach (Movie item in MovieManage.dirMovie.Values)
            {
                if (item.MovieName.IndexOf(name) != -1)
                {
                    dirmovie.Add(item.MovieId, item);
                    break;
                }
            }
            dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = new BindingList<Movie>(dirmovie.Values.ToList());

        }
        
        //新增
        private void button2_Click(object sender, EventArgs e)
        {
            FrmAddMovie add = new FrmAddMovie();
            add.n = 1;
            add.Show();
            add.FormClosed += add_FormClosed;
        }

        //修改
        private void button4_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            FrmAddMovie add = new FrmAddMovie();
            add.n = 2;
            add.movie = MovieManage.dirMovie[id];
            add.Show();
            add.FormClosed += add_FormClosed;
        }

        void add_FormClosed(object sender, FormClosedEventArgs e)
        {
            BindData();
        }

        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            DialogResult r = MessageBox.Show("确定删除编号为《"+id+"》的影片吗？","提示：",
                MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
            if (r != DialogResult.OK)
                return;
            MovieManage.dirMovie.Remove(id);
            BindData();
        }
        #endregion

        #region 关闭窗体
        //关闭窗体发生
        private void FrmMovieAdmin_FormClosed(object sender, FormClosedEventArgs e)
        {
            MovieManage.SerializeMovie();
        }
        //退出
        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 场次管理
        //场次管理
        private void button9_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            FrmMoviePlay play = new FrmMoviePlay();
            play.movie = MovieManage.dirMovie[id];
            play.Show();
        }
        #endregion

    }
}
